const fs = require('fs-extra');
const path = require('path');
const Web3 = require('web3');
const HDWalletProvider = require('truffle-hdwallet-provider');

// 1.拿到bytecode
const contractPath = path.resolve(__dirname,'../compiled/ProjectList.json');
const { interface, bytecode } =  require(contractPath);

// 2.配置provider
const provider = new HDWalletProvider(
  'retire draw amount runway alpha mind quick burden chalk hazard wonder soul',
'https://rinkeby.infura.io/nrUNwM9Fa0GkeCy0VS3N');

// 3.初始化web3实例
const web3 = new Web3(provider);

(async () => {
  // 4.获取钱包里的账户
  const accounts = await web3.eth.getAccounts();
  console.log('合约部署账户：', accounts[0]);

  // 5.合建合约实例并部署
  console.time('合约部署耗时');
  const result = await new web3.eth.Contract(JSON.parse(interface))
    .deploy({data: bytecode})
    .send({from: accounts[0], gas: '5000000'});
  console.timeEnd('合约部署耗时');

  const contractAddress = result.options.address;

  console.log('合约部署成功：', contractAddress);
  console.log('合约查看地址：', `https://rinkeby.etherscan.io/address/${contractAddress}`);

  const addressFile = path.resolve(__dirname, '../address.json');
  fs.writeFileSync(addressFile, JSON.stringify(contractAddress));
  console.log('地址写入成功：', addressFile);

  // ???? 
  process.exit();
})();